package shop.order.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import shop.common.entity.Order;
import shop.common.entity.Product;
import shop.order.service.OrderService;
import shop.order.service.ProductService;

import java.util.List;
import java.util.Random;

@RestController
@Slf4j
@Api(tags = "订单服务")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private OrderService orderService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private ProductService productService;

    //准备买1件商品
    @GetMapping("/prod/{pid}")
    @ApiOperation(value = "客户下单")
    public Order orderOpenfeign(@PathVariable("pid") Integer pid) {

        log.info(">>客户下单，这时候要调用商品微服务查询商品信息");

        //通过fegin调用商品微服务
        Product product = productService.findByPid(pid);


        log.info(">>商品信息，查询结果:" + JSON.toJSONString(product));

        Order order = new Order();
        order.setUId(1);
        order.setUserName("测试用户");
        order.setPId(product.getPId());
        order.setPName(product.getPName());
        order.setPPrice(product.getPPrice());
        order.setNumber(1);

        orderService.save(order);
        return order;
    }


    //准备买1件商品
    public Order order6(@PathVariable("pid") Integer pid) {

        log.info(">>客户下单，这时候要调用商品微服务查询商品信息");

        //直接使用微服务名字， 从nacos中获取服务地址
        String url = "serviceproduct";
        log.info(">>从nacos中获取到的微服务地址为:" + url);

        //通过restTemplate调用商品微服务
        Product product = restTemplate.getForObject("http://" + url + "/product/" + pid, Product.class);

        log.info(">>商品信息，查询结果:" + JSON.toJSONString(product));

        Order order = new Order();
        order.setUId(1);
        order.setUserName("测试用户");
        order.setPId(product.getPId());
        order.setPName(product.getPName());
        order.setPPrice(product.getPPrice());
        order.setNumber(1);

        orderService.save(order);
        return order;
    }

    //准备买1件商品
    public Order order5(@PathVariable("pid") Integer pid) {

        log.info(">>客户下单，这时候要调用商品微服务查询商品信息");

        //从nacos中获取服务地址
        //自定义规则实现随机挑选服务
        List<ServiceInstance> instances = discoveryClient.getInstances("serviceproduct");
        int index = new Random().nextInt(instances.size());
        ServiceInstance serviceInstance = instances.get(index);

        String url = serviceInstance.getHost() + ":" + serviceInstance.getPort();
        log.info(">>从nacos中获取到的微服务地址为:" + url);

        //通过restTemplate调用商品微服务
        Product product = restTemplate.getForObject("http://" + url + "/product/" + pid, Product.class);

        log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
        Order order = new Order();
        order.setUId(1);
        order.setUserName("测试用户");
        order.setPId(product.getPId());
        order.setPName(product.getPName());
        order.setPPrice(product.getPPrice());
        order.setNumber(1);

        orderService.save(order);
        return order;
    }

    //准备买1件商品
    public Order order3(@PathVariable("pid") Integer pid) {

        log.info(">>客户下单，这时候要调用商品微服务查询商品信息");

        //从nacos中获取服务地址
        ServiceInstance serviceInstance = discoveryClient.getInstances("serviceproduct").get(0);

        String url = serviceInstance.getHost() + ":" + serviceInstance.getPort();
        log.info(">>从nacos中获取到的微服务地址为:" + url);

        //通过restTemplate调用商品微服务
        Product product = restTemplate.getForObject(
                "http://" + url + "/product/" + pid, Product.class);

        log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
        Order order = new Order();
        order.setUId(1);
        order.setUserName("测试用户");
        order.setPId(product.getPId());
        order.setPName(product.getPName());
        order.setPPrice(product.getPPrice());
        order.setNumber(1);

        orderService.save(order);
        return order;
    }

    public Order order2(@PathVariable("pid") Integer pid) {

        log.info(">>客户下单，这时候要调用商品微服务查询商品信息");

        //通过restTemplate调用商品微服务
        Product product = restTemplate.getForObject(
                "http://localhost:8071/product/" + pid, Product.class);

        log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
        Order order = new Order();
        order.setUId(1);
        order.setUserName("测试用户");
        order.setPId(product.getPId());
        order.setPName(product.getPName());
        order.setPPrice(product.getPPrice());
        order.setNumber(1);

        orderService.save(order);
        return order;
    }
}


